package org.makcro.sms.services.chat.web.client.widgets.main;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.makcro.sms.services.chat.web.client.widgets.DeliveryReportGridCellRendererImpl;
import org.makcro.sms.services.chat.web.client.widgets.HistoryCellRenderer;
import org.makcro.sms.services.chat.web.client.widgets.bulk.BulkContextMenu;
import org.makcro.sms.services.chat.web.client.widgets.bulk.SendBulkMenuItemSelectionListener;
import org.makcro.sms.services.chat.web.shared.dto.HistoryItemDto;

import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BasePagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadConfig;
import com.extjs.gxt.ui.client.data.PagingLoadResult;
import com.extjs.gxt.ui.client.data.PagingLoader;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.store.GroupingStore;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GroupingView;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.google.gwt.i18n.client.DateTimeFormat;

public class AnsweredMessagesPanel extends ContentPanel {

	private Grid<HistoryItemDto>			grid;
	private GroupingStore<HistoryItemDto>	store;
	private List<ColumnConfig>				columns;
	private BulkContextMenu					bulkContextMenu;
	private GroupingView					groupingView;
	private PagingToolBar					pagingToolBar;
	private DateTimeFormat					dateTimeFormat	= DateTimeFormat.getFormat( "dd/MM/yyyy 'u' HH:mm" );
	private PagingLoader<PagingLoadResult<HistoryItemDto>> pagingLoader;
	
	public AnsweredMessagesPanel() {

		initComponents();
	}

	public void initComponents() {
		setLayout( new FitLayout() );
		
		
	}
	
	public void init(){
		getPagingToolBar().bind( getPagingLoader() );
		setBottomComponent( getPagingToolBar() );
		
		add( getGrid() );
	}

	public Grid<HistoryItemDto> getGrid() {
		if (grid == null) {
			grid = new Grid<HistoryItemDto>( getStore(), new ColumnModel( getColumns() ) );
			grid.setAutoExpandColumn( "questoin" );
			grid.setContextMenu( getBulkContextMenu() );
			grid.getSelectionModel().setSelectionMode( SelectionMode.SINGLE );
			grid.setView( getGroupingView() );
			grid.disableTextSelection( false );
			grid.setLoadMask( true );
			grid.addListener( Events.Attach, new Listener<GridEvent<HistoryItemDto>>() {

				@Override
				public void handleEvent(GridEvent<HistoryItemDto> be) {
					// setup paging params
					PagingLoadConfig pagingLoadConfig = new BasePagingLoadConfig();
					pagingLoadConfig.setLimit( 50 );
					pagingLoadConfig.setOffset( 0 );
					
					Map<String, Object> state = grid.getState();  
			        if (state.containsKey("offset")) {  
			          int offset = (Integer)state.get("offset");  
			          int limit = (Integer)state.get("limit");  
			          pagingLoadConfig.setOffset(offset);  
			          pagingLoadConfig.setLimit(limit);  
			        }  
			        // load data
			        getPagingLoader().load();
				}
			});
		}
		return grid;
	}

	public GroupingStore<HistoryItemDto> getStore() {
		if (store == null) {
			store = new GroupingStore<HistoryItemDto>(this.pagingLoader);
		}
		return store;
	}

	public List<ColumnConfig> getColumns() {
		if (columns == null) {
			columns = new ArrayList<ColumnConfig>();
			ColumnConfig messageColumn = new ColumnConfig( "questoin", "Poruka", 300 );
			messageColumn.setRenderer( new HistoryCellRenderer() );
			messageColumn.setGroupable( false );
			messageColumn.setMenuDisabled( true );

			// from number
			ColumnConfig fromColumn = new ColumnConfig( "fromNumber", "Primalac", 50 );

			// delivery status
			ColumnConfig deliveryStatusColumn = new ColumnConfig( "answerDelivered", "Isporucena", 30 );
			deliveryStatusColumn.setRenderer( new DeliveryReportGridCellRendererImpl() );

			// date answered
			ColumnConfig answerDateColumn = new ColumnConfig( "dateAnswered", "Datum odgovora", 50 );
			answerDateColumn.setDateTimeFormat( this.dateTimeFormat );

			ColumnConfig answerId = new ColumnConfig( "answerId", "#", 50 );
			// add columns
			columns.add( answerId );
			columns.add( fromColumn );
			columns.add( answerDateColumn );
			columns.add( deliveryStatusColumn );
			columns.add( messageColumn );
		}
		return columns;
	}

	public BulkContextMenu getBulkContextMenu() {
		if (this.bulkContextMenu == null) {
			this.bulkContextMenu = new BulkContextMenu();
			this.bulkContextMenu.getSendBulkMenuItem().addListener( Events.Select, new SendBulkMenuItemSelectionListener( grid ) );
		}
		return bulkContextMenu;
	}

	public GroupingView getGroupingView() {
		if (this.groupingView == null) {
			this.groupingView = new GroupingView();
			this.groupingView.setShowGroupedColumn( true );
			this.groupingView.setForceFit( true );

		}
		return groupingView;
	}
	
	public PagingToolBar getPagingToolBar() {
		if (this.pagingToolBar == null) {
			pagingToolBar = new PagingToolBar( 50 );
		}
		return pagingToolBar;
	}
	
	public PagingLoader<PagingLoadResult<HistoryItemDto>> getPagingLoader() {
		return pagingLoader;
	}
	
	public void setPagingLoader(PagingLoader<PagingLoadResult<HistoryItemDto>> pagingLoader) {
		this.pagingLoader = pagingLoader;
	}

}
